import goodsApi from '@/api/shop/goods'
import { getApiUrl } from '@/utils/utils'
import permission from '@/directive/permission/index.js'

export default {
  directives: { permission },
  data() {
    return {
      listQuery: {
        page: 1,
        limit: 20,
        name: ""
      },
      total: 0,
      list: null,
      listLoading: true,
      selRow: {},
      apiUrl: getApiUrl(),
      formVisible: false,
      formTitle: '更新库存',
      buttonText: '审核不通过',
      form: {
        id: 0,
        stock: 0,
      },
    }
  },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'gray',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  created() {
    this.init()
  },
  methods: {
    init() {
      this.fetchData(0)
    },
    fetchData(audit) {
      this.listLoading = true
      this.listQuery.isAudit = audit
      let t = {
        // createBy: 0,
        isAudit: audit
      }
      goodsApi.getAuditList(t).then(response => {
        //console.log('列表数据：', response.data.records);
        for (let index in response.data.records) {
          if (response.data.records[index].isAudit === 1) {
            response.data.records[index].isAudit = true
            //console.log("false" + response.data.records[index].isAudit)
          } else {
            response.data.records[index].isAudit = false
            //console.log("true" + response.data.records[index].isAudit)
          }
        }
        this.list = response.data.records
        this.listLoading = false
        this.total = response.data.total
      })
    },
    handleCommand(command) {
      this.fetchData(command)
      if (command == 1) {
        this.buttonText = "审核通过"
      } else {
        this.buttonText = "审核不通过"
      }
    },
    search() {
      this.listQuery.page = 1
      this.fetchData()
    },
    reset() {
      this.listQuery.name = ''
      this.listQuery.page = 1
      this.fetchData()
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    handleClose() {

    },
    fetchNext() {
      this.listQuery.page = this.listQuery.page + 1
      this.fetchData()
    },
    fetchPrev() {
      this.listQuery.page = this.listQuery.page - 1
      this.fetchData()
    },
    fetchPage(page) {
      this.listQuery.page = page
      this.fetchData()
    },
    changeSize(limit) {
      this.listQuery.limit = limit
      this.fetchData()
    },
    handleCurrentChange(currentRow, oldCurrentRow) {
      //console.log("输出一下");
      this.selRow = currentRow
    },
    checkSel() {
      if (this.selRow && this.selRow.id) {
        return true
      }
      this.$message({
        message: this.$t('common.mustSelectOne'),
        type: 'warning'
      })
      return false
    },
    // 新增
    add() {
      this.$router.push('goodsEdit')
    },
    // 编辑
    edit(id) {
      this.$router.push({ path: 'goodsEdit', query: { id: id } })
    },
    changeIsOnSale(data) {
      goodsApi.changeIsOnSale(data.id, data.isOnSale).then(response => {
        this.$message({
          message: data.isOnSale ? '上架成功' : '下架成功',
          type: 'success'
        })
      })

    },
    updateStockForm(param) {
      this.form = {
        id: param.id,
        stock: param.stock,
      }
    },
    updateStock(param) {
      this.updateStockForm(param)
      this.formTitle = '更新库存'
      this.formVisible = true
    },
    updateStockSave() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          goodsApi.apiUpdateStockSave({
            id: this.form.id,
            stock: this.form.stock,
          }).then(response => {
            this.$message({
              message: this.$t('common.optionSuccess'),
              type: 'success'
            })
            this.fetchData()
            this.formVisible = false
          })
        } else {
          return false
        }
      })
    },
    changeIsAudit(data) {
      let audit = 0
      if (data.isAudit) {
        audit = 1
      } else {
        audit = 0
      }
      goodsApi.changeIsAudit(data.id, audit).then(response => {
        this.$message({
          message: data.isAudit ? '审核通过' : '审核不通过',
          type: 'success'
        })
      })

    },
  }
}
